<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>智慧社区后台-党员风采</title>
<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
<link rel="stylesheet" href="${pageContext.request.contextPath}/plugins/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/AdminLTE.min.css">
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/plugins/bootstrap-table/css/bootstrap-table.css">
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/plugins/bootstrap-switch/css/bootstrap-switch.min.css">
<link rel="stylesheet" href="${pageContext.request.contextPath}/kindeditor-4.1.10/themes/default/default.css" rel="stylesheet" type="text/css" />
</head>
<style>
#mienContent{
	text-align: center;
}
#mienContent img{
	width: 60%;
    height: auto;
    margin: 20px;
}
</style>
<body style="padding-bottom: 90px; background-color: #ECF0F5;">
	<section class="content" style="margin: 15px; background-color: white;">
		<div class="row">
			<div class="col-sm-12">
				<table id="table">
					<thead style="background-color: #00C0EF">
						<tr style="background-color: #00C0EF">
							<th class="text-center" data-field=""></th>
							<th class="text-center" data-field="mTitle">标题</th>
							<th class="text-center" data-field="mTime">创建时间</th>
							<th class="text-center" data-field="mProple">操作员</th>
							<th class="text-center" data-field="mReleaseTime">发布时间</th>
							<th class="text-center" data-field="mState">发布状态</th>
							<th class="text-center" data-field="mId">操作</th>
						</tr>
					</thead>
				</table>
			</div>
		</div>
	</section>
	<!-- Large modal -->
	<div id="modal" class="modal fade bs-example-modal-lg" role="dialog" aria-labelledby="myLargeModalLabel">
		<div class="modal-dialog modal-lg" role="document">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal" aria-label="Close">
						<span aria-hidden="true">&times;</span>
					</button>
					<h4 class="modal-title">党员风采</h4>
				</div>
				<div class="modal-body">
					<div class="form-group">
						<label for="mien_title" class="control-label">风采主题:</label> <input type="text" class="form-control" id="mien_title">
					</div>
					<div class="form-group">
						<label for="mien_pic" class="control-label">标题图片:</label>
						<button id="imgBtn" class="btn btn-primary glyphicon glyphicon-plus" onclick="imgFile.click();">上传图片</button>
						<img id="image" class="cover-radius"  src="${pageContext.request.contextPath}/img/upload.jpg"
		                           width="150px;" style="cursor: pointer;" onclick="imgFile.click();"/>
	                    <input type="file" class="form-control" style="display: none;" name="imgFile" id="imgFile"
	                           onchange="fileChange()">
		           	</div>
					<div class="form-group">
						<label for="user_name" class="control-label">操作员:</label> <input type="text" class="form-control" id="user_name" value="" disabled="disabled">
					</div>
					<div class="form-group">
						<label for="mien_content" class="control-label">内容:</label>
						<textarea id="mien_content" name="mien_content" style="width: 100%;height: 350px;"></textarea>
					</div>
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
					<button id=submitBtn type="button" class="btn btn-primary" onclick="submitMien();">提交</button>
				</div>
			</div>
		</div>
	</div>
	<div id="enrollModal"  class="modal fade bs-example-modal-lg" role="dialog" aria-labelledby="myLargeModalLabel">
		<div class="modal-dialog modal-lg" role="document">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal" aria-label="Close">
						<span aria-hidden="true">&times;</span>
					</button>
					<h4 class="modal-title">党员风采</h4>
				</div>
				<div class="modal-body">
					<div class="row">
						<div class="col-sm-12">
							<div style="overflow: hidden;">
								<div style="text-align:center;">
									<h3 id="mienTitle">标题</h3>
								</div>
								<div style="float: right;">
									<span style="">发布人：</span><span id="userName">默认</span>
									<h5 id="mienTime">时间</h3>
								</div>
							</div>
							<hr style="color: gray;"/>
							<div style="align-content: center;">
								<div id="mienContent"></div>
							</div>
						</div>
					</div>
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
				</div>
			</div>
		</div>
	</div>
</body>
<script src="${pageContext.request.contextPath}/plugins/jQuery/jquery-2.2.3.min.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap/js/bootstrap.min.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/bootstrap-table.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/bootstrap-table-zh-CN.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/bootstrap-table-export.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/tableExport.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/bootstrap-table-toolbar.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/bootstrap-table-filter-control.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-switch/js/bootstrap-switch.min.js"></script>
<script src="${pageContext.request.contextPath}/js/ajaxfileupload.js"></script>
<!-- layer -->
<script src="${pageContext.request.contextPath}/plugins/layer/2.4/layer.js"></script>
<script src="${pageContext.request.contextPath}/plugins/laydate/laydate.js"></script>
<!-- kindeditor -->
<script src="${pageContext.request.contextPath}/kindeditor-4.1.10/kindeditor-min.js"></script>
<script src="${pageContext.request.contextPath}/kindeditor-4.1.10/lang/zh_CN.js"></script>
<script src="${pageContext.request.contextPath}/js/date.js"></script>
<script>
	var editor;
	var mId = "";
	var mienId = "";
	
	function actionFormatter(value, row, index) {
		return [ "<button class='btn btn-success btn-xs glyphicon glyphicon-file' onclick='showInfo(\"" + row.mId + "\");'>查看详情</button>",
			"&nbsp;<button class='btn btn-primary btn-xs glyphicon glyphicon-edit' onclick='showModal(\"" + row.mId + "\");'>编辑信息</button>",
			"&nbsp;<button class='btn btn-warning btn-xs glyphicon glyphicon-trash' onclick='delMien(\"" + row.mId + "\")'>删除信息</button>", ].join('');
	}
	var table_dataset = [ {
		field : 'id',
		checkbox : true,
	}, {
		field : 'mTitle',
		align : 'center',
	}, {
		field : 'mTime',
		align : 'center',
		formatter : function timeFormatter(value, row, index) {
			return (new Date(row.mTime)).Format("yyyy-MM-dd hh:mm:ss");
		}
	}, {
		field : 'mProple',
		align : 'center',
	},  {
		field : 'mReleaseTime',
		align : 'center',
		formatter : function timeFormatter(value, row, index) {
			return (new Date(row.mReleaseTime)).Format("yyyy-MM-dd hh:mm:ss");
		}
	},  {
		field : 'mState',
		width : 50,
		align : 'center',
		formatter : actionMienState,
	} ,{
		align : 'center',
		width : 300,
		formatter : actionFormatter,
	} ];
	$('#table').bootstrapTable({
		url : "${pageContext.request.contextPath}/partyMemberMien/selectPartyMemberMienPage",
		idField : "mId",
		columns : table_dataset,
		striped : true, //是否显示行间隔色
		pagination : true, //是否显示分页
		pageSize : 10, //每页的记录行数
		pageList : [ 10, 25, 50, 100 ], //可供选择的每页的行数
		search : false, //是否显示搜索
		showRefresh : true,
		sidePagination : "server", //表示服务端请求  
		queryParamsType : "limit",
		queryParams : function queryParams(params) { //设置查询参数  
			var param = {
				offset : params.offset,
				limit : params.limit,
				kod	  : $("#kod").val()
			};
			return param;
		},
		toolbar : "<button class='btn btn-primary glyphicon glyphicon-plus' onclick='showModal(\"" + '' + "\");'>新增数据</button>"
		+"<div class='input-group' style='width:250px;float:right;margin-top:2px;margin-left:20px;'><input type='text' class='form-control' placeholder='请输入标题' id='kod' name='kod'>"
		+ "<span class='input-group-btn'><button class='btn btn-default' type='button' onclick='Querry(\"" + '' + "\");'>查询</button></span></div>",
		icons : {
			paginationSwitchDown : 'glyphicon-collapse-down icon-chevron-down',
			paginationSwitchUp : 'glyphicon-collapse-up icon-chevron-up',
			refresh : 'glyphicon-refresh icon-refresh',
			toggle : 'glyphicon-list-alt icon-list-alt',
			columns : 'glyphicon-th icon-th',
			detailOpen : 'glyphicon-plus icon-plus',
			detailClose : 'glyphicon-minus icon-minus',
			"export" : 'glyphicon-export icon-share'
		},
		showExport : true,
		exportDataType : 'basic',
		exportTypes : [ 'json', 'xml', 'csv', 'txt', 'sql', 'excel' ]
	});
	
	function actionMienState(value, row, index){
		//console.log(row);
		if(row.mState==0){
			return [ "<button class='btn btn-danger btn-xs glyphicon ' onclick='releaseState(\"" + row.mId+ "\",\""+row.mState+"\")'>未发布</button>",].join('');
		}else{
			return [ "<button class='btn btn-success btn-xs glyphicon ' onclick='releaseState(\"" + row.mId+"\",\""+row.mState+"\")'>已发布</button>",].join('');
		}
		
	}
	
	function releaseState(id,state) {
		console.log(state);
		var mState;
		if(state==0){
			mState="1";
		}else if(state==1){
			mState="0";
		}
			$.ajax({
				url : "${pageContext.request.contextPath}/partyMemberMien/updatePartyMemberMienState",
				type : "post",
				data : {
					mId : id,
					mState:mState
				},
				dataType : "json",
				success : function(data) {
					if (data.result === 1) {
						layer.msg('操作成功!', {
							icon : 1,
							time : 800
						});
						setTimeout(function() {
							$("#table").bootstrapTable('refresh');
						}, 500);
					} else {
						layer.msg('操作失败!', {
							icon : 2,
							time : 800
						});
					}
				},
				error : function() {
					layer.msg('系统繁忙，请稍后再试!', {
						icon : 5,
						time : 800
					});
				}
			});
	}

	
	function showModal(id) {
		console.log(id);
		if (id == "") {//新增
			mId = '';
			mienId = guid();
			KindEditor.ready(function(K) {
			    K.remove('textarea[name="mien_content"]');
			});
			options.extraFileUploadParams = {folderId : mienId};
			//kindeditor初始化
			KindEditor.ready(function(K) {
				    editor = K.create('textarea[name="mien_content"]', options); 
			});
			$("#mien_title").val('');
			imgUrl = "";
			$("#imgBtn").show();
			$('#image').attr('src','');
			$("#user_name").val("${sessionScope.userAccount}");
			editor.html("");
			$('#modal').modal({
				backdrop : 'static',
				keyboard : false
			});
		} else {//修改
			mId = id;
			mienId = id;
			KindEditor.ready(function(K) {
			    K.remove('textarea[name="mien_content"]');
			});
			options.extraFileUploadParams = {folderId : mienId};
			//kindeditor初始化
			KindEditor.ready(function(K) {
				    editor = K.create('textarea[name="mien_content"]', options); 
			});
			$.ajax({
				url : "${pageContext.request.contextPath}/partyMemberMien/selectPartyMemberMienById",
				type : "get",
				data : {
					mId : mId
				},
				dataType : "json",
				success : function(data) {
					var mien = data.result;
					$("#mien_title").val(mien.mTitle);
					$('#image').attr('src','');
					if(mien.mImg!=null && mien.mImg!=""){
						imgUrl = mien.miePic;
						$('#image').attr('src','${pageContext.request.contextPath}/'+mien.mImg);
						$("#imgBtn").hide();
						$('#image').show();
					}else{
						$("#imgBtn").show();
					}
					editor.html(mien.mContent);
					$('#modal').modal({
						backdrop : 'static',
						keyboard : false
					});
				},
				error : function() {
					layer.msg('系统异常!', {
						icon : 2,
						time : 800
					});
				}
			});
		}
	}
	
	//显示详情
	function showInfo(id) {
		mId = id;
		mienId = id;
		$.ajax({
			url : "${pageContext.request.contextPath}/partyMemberMien/selectPartyMemberMienById",
			type : "get",
			data : {
				mId : mId
			},
			dataType : "json",
			success : function(data) {
				var mien = data.result;
				$("#mienTitle").html(mien.mTitle);
				$("#userName").html(mien.mProple);
				$("#mienTime").html(mien.mTime);
				$("#mienContent").html(mien.mContent);
				$('#enrollModal').modal({
					backdrop : 'static',
					keyboard : false
				});
			},
			error : function() {
				layer.msg('系统异常!', {
					icon : 2,
					time : 800
				});
			}
		});
	}
	
	//保存
	function submitMien() {
		$("#submitBtn").button('loading');
		editor.sync();  
		var mTitle = $("#mien_title").val();
		var mProple = $("#user_name").val();
		var mContent = $("#mien_content").val();
		var mImg = imgUrl;
		if (mTitle === '' || mImg === '' || mContent === '' ) {
			layer.alert("新增的数据中填写不完整!");
			$("#submitBtn").button('reset');
			return false;
		}
		if (editor.isEmpty()) {
			layer.alert("新增的数据中填写不完整!");
			$("#submitBtn").button('reset');
			return false;
		}
		var url = mId === '' ? '${pageContext.request.contextPath}/partyMemberMien/insertPartyMemberMien' : '${pageContext.request.contextPath}/partyMemberMien/updatePartyMemberMien';
		$.ajax({
			url : url,
			type : "post",
			data : {
				mId : mienId,
				mTitle : mTitle,
				mProple : mProple,
				mContent : mContent,
				mImg : mImg
			},
			dataType : "json",
			success : function(data) {
				var result = data['result'];
				if (result === 1) {
					layer.msg('操作成功!', {
						icon : 1,
						time : 800
					});
					setTimeout(function() {
						$("#table").bootstrapTable('refresh');
						$("#modal").modal("hide");
					}, 1000);
				} else {
					layer.msg('操作失败!', {
						icon : 2,
						time : 800
					});
				} 
				$("#submitBtn").button('reset');
			},
			error : function() {
				layer.msg('系统繁忙，请稍后再试!', {
					icon : 2,
					time : 800
				});
				$("#submitBtn").button('reset');
			}
		}); 

		return false;
	}

	//取删除风采
	function delMien(id) {
		layer.confirm("是否要删除信息？", function(index) {
			$.ajax({
				url : "${pageContext.request.contextPath}/partyMemberMien/deletePartyMemberMien",
				type : "get",
				data : {
					mId : id
				},
				dataType : "json",
				success : function(data) {
					if (data.result == 1) {
						$("#table").bootstrapTable('refresh');
						layer.msg('操作成功!', {
							icon : 1,
							time : 800
						});
					} else {
						layer.msg('操作失败!', {
							icon : 2,
							time : 800
						});
					}
				},
				error : function() {
					layer.msg('系统繁忙，请稍后再试!', {
						icon : 5,
						time : 800
					});
				}
			});
		});
	}
	var options = {
			resizeType : 0,  
			//uploadJson : 'lib/kindeditor/upload/upload_json.jsp',  
			uploadJson : "upload_img",//需要加上basePath, 不然批量上传调用action时会出现路径问题  
			extraFileUploadParams : {
				mienId : mienId
        	},
			urlType: "absolute",
			fileManagerJson : 'mien_file_manager',  
			allowImageUpload : true,  
			allowFileManager : false,  
			allowPreviewEmoticons : false, 
			items : [  
			    'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'subscript', 'superscript', 'bold', 'italic', 'underline',  
			    'removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist',  
			    'insertunorderedlist', '|', 'emoticons', 'image','multiimage', 'link', '|', 'preview', 'source', 'fullscreen'],
		    afterUpload : function(url) {
		    	
	        },
	    	afterBlur : function() {   
			    this.sync();  //焦点问题，这里不写会出问题.同步KindEditor的值到textarea文本框      
			}
	};
	//kindeditor初始化
	KindEditor.ready(function(K) {
		    editor = K.create('textarea[name="mien_content"]',options); 
	 });
	
	function fileChange() {
        if (image_check('imgFile')) { //自己添加的文件后缀名的验证
            $.ajaxFileUpload({
                fileElementId: 'imgFile',    //需要上传的文件域的ID，即<input type="file">的ID。
                url: "${pageContext.request.contextPath}/upload_img?folderId="+mienId, //后台方法的路径
                type: 'post',   //当要提交自定义参数时，这个参数要设置成post
                dataType: 'json',   //服务器返回的数据类型。可以为xml,script,json,html。如果不填写，jQuery会自动判断。
                secureuri: false,   //是否启用安全提交，默认为false。
                async : true,   //是否是异步
                success: function(data) {   //提交成功后自动执行的处理函数，参数data就是服务器返回的数据。
                	imgUrl = data.url;
                	$('#image').attr('src','${pageContext.request.contextPath}/'+data.url);
                	$("#image").show();
                	$("#imgBtn").hide();
                },
                error: function(data, status, e) {  //提交失败自动执行的处理函数。
                    console.error(e);
                }
            });
        }
    }
	
    function image_check(feid) { //自己添加的文件后缀名的验证
        var img = document.getElementById(feid);
        return /.(jpg|png|gif|bmp)$/.test(img.value)?true:(function() {
        	layer.alert('图片格式仅支持jpg、png、gif、bmp格式，且区分大小写。');
            return false;
        })();
    }
    
    function Querry(){
		 $("#table").bootstrapTable('refresh');
	}
    
    function guid() {
        function S4() {
           return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
        }
        return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
    }
</script>
</html>
